(function(){
	var webrtc = {
		socket:null,
		events:{},
		init:function(ip,name){
			this.socket = io(ip)
			this.allEvents()
			this.join(name)
		},
		join:function(name){
			this.socket.emit('join',name)
		},
		call:function(self,names){
			this.socket.emit('call',{caller:self,all:JSON.stringify(names)})
		},	
		uncall:function(self,names){
			this.socket.emit('uncall',{caller:self,all:JSON.stringify(names)})
		},
		acceptCall:function(data){
			this.socket.emit('accept',data)
		},
		refuseCall:function(data){
			this.socket.emit('refuse',data)
		},
		on:function(events){
			this.events = events
		},
		emit:function(ev,data){
			this.events[ev](data)
		},
		allEvents:function(){
			var _this = this
			this.socket.on('subcall',function(data){
				_this.emit('listenCall',data)
			})
			this.socket.on('fail',function(data){
					_this.emit('notOnline',data)
				})
			this.socket.on('anserRefuse',function(data){
				    _this.emit('listenRefuse',data.accepter)
				})
			this.socket.on('failRoom',function(val){
					_this.emit('alreadyRoom',val)
				})
			this.socket.on('showvd',function(data){
//                  window.location = 'https://192.168.43.89:8443/rtc.html?id='+data.roomNum  
                    _this.emit('allAccept',data)
				})
			this.socket.on('listenUncall',function(data){
			//                  window.location = 'https://192.168.43.89:8443/rtc.html?id='+data.roomNum  
			        _this.emit('listenUncall',data)
							})
			this.socket.on('readyCall', function(data){
			     _this.emit('readyCall',data)
			});					
		}			
	}
	this.webrtc = webrtc
})()

            
     
			
			